package com.sendinfo.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;
import com.opensymphony.webwork.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class ActionBase extends ActionSupport implements Preparable, SessionAware, ServletRequestAware, ServletResponseAware {
	private static final long serialVersionUID = 4833974912206849234L;
	protected static final String THEME_ROOT = "theme";
	protected String theme = "default";
	protected static final String MESSAGE = "message";
	protected static final String REDIRECT = "redirect";
	private Map<String, Object> session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected String base;
	protected String message;
	private String javascript;
	private String title = "宋城电子商务";

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getJavascript() {
		return javascript;
	}

	public void setJavascript(String javascript) {
		this.javascript = javascript;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getTheme() {
		// return THEME_ROOT + "/" + theme;
		return theme.startsWith(THEME_ROOT + "/") ? theme : THEME_ROOT + "/" + theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}


	public void prepare() throws Exception {}

	@SuppressWarnings("unchecked")
	public void setSession(Map session) {
		this.session = session;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	protected boolean anonymousAccess = false;

	public boolean isAnonymousAccess() {
		return anonymousAccess;
	}

	public void setAnonymousAccess(boolean anonymousAccess) {
		this.anonymousAccess = anonymousAccess;
	}

	/**
	 * @Function:ajxa 返回页面json格式文本数据
	 * @author: ww
	 * @date: 2008-12-10 下午04:04:31
	 * @param str
	 */
	public void outJsonString(String str) {
		this.getResponse().setContentType("text/javascript;charset=UTF-8");
		outString(str);
	}

	/**
	 * @Function:write给rpc调用者
	 * @author: ww
	 * @date: 2008-12-10 下午03:32:43
	 * @param str
	 */
	public void outString(String str) {
		try {
			PrintWriter out = this.getResponse().getWriter();
			out.write(str);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Function:ajax 返回页面xml格式文本数据
	 * @author: ww
	 * @date: 2008-12-10 下午04:06:32
	 * @param xmlStr
	 */
	public void outXMLString(String xmlStr) {
		this.getResponse().setContentType("application/xml;charset=UTF-8");
		outString(xmlStr);
	}

	private String getContextPath() {
		return this.getRequest() == null ? "" : this.getRequest().getContextPath();
	}

	public String getBase() {
		if (null == base) base = getContextPath();
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}
}
